package com.czf.testmodule.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author zfChen
 * @create 2022/6/6 12:12
 */
public class FileChannelDemo {
    public static void main(String[] args) {
        try {
            RandomAccessFile aFile = new RandomAccessFile("E:\\文档\\桌面\\123.txt", "rw");
            FileChannel channel = aFile.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(48);
            int read = channel.read(buffer);
            while (read != -1) {
                System.out.println("读取："+read);
                buffer.flip();
                while (buffer.hasRemaining()) {
                    System.out.println((char) buffer.get());
                }
                buffer.clear();
                read = channel.read(buffer);
            }
            aFile.close();
            System.out.println("操作结束");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
